//
//  NavigationVC.m
//  Echo
//
//  Created by Cospring on 14/12/17.
//  Copyright (c) 2014年 sina. All rights reserved.
//

#import "NavigationVC.h"
#import "ApiConfig.h"
#import "UIConfig.h"
#import "CurrentUser.h"

@interface NavigationVC ()

@end

@implementation NavigationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
//    if (!UIAccessibilityIsReduceTransparencyEnabled()) {
//        NSLog(@"%@", @"yes");
//        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
//        UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
////        [blurEffectView setAlpha:0.5];
//        blurEffectView.frame = self.view.bounds;
//        [self.view addSubview:blurEffectView];
//    } else {
//        
//    }
    
    
    // Do any additional setup after loading the view from its nib.
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(closeNavigation:)];
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipe];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeNavigation:)];
    [self.view addGestureRecognizer:tap];
    
    
    
    NSDictionary *TAG_NAME_HASH = @{
       @"Hadoop":      CATE_HADOOP,
       @"Java":        CATE_JAVA,
       @"PHP":         CATE_PHP,
       @"Javascript":  CATE_JAVASCRIPT,
       @"OC":          CATE_OC,
       @"Python":      CATE_PYTHON,
       @"C++":         CATE_CPP,
       @"Perl":        CATE_PERL,
       @"C":           CATE_C,
       @"R":           CATE_R,
       @"Swift":       CATE_SWIFT
    };
    
    NSArray *TAG_IMAGE_LIST = @[@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n"];
    
    CGFloat tagX = 68;
    CGFloat tagY = 0;
//    CGFloat tagWidth = self.tagItemView.frame.size.width;
    CGFloat tagWidth = 100;
    CGFloat tagHeight = 35;
    
    CGFloat tagImageX = 25;
    CGFloat tagImageY = 5;
    CGFloat tagImageWidth = 18;
    CGFloat tagImageHeight = 25;
    
    NSUInteger index = 0;
    for (NSString *cate in TAG_NAME_HASH) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(tagX, tagY, tagWidth, tagHeight)];
//        label.font = [UIFont fontWithName:@"SourceSansPro-Light" size:18];
        label.font = [UIFont fontWithName:@"SourceSansPro-ExtraLight" size:20];
        label.textColor = [UIColor whiteColor];
        label.text = cate;
        tagY += 50;
        
        CGSize fitSize = [label sizeThatFits:CGSizeMake(tagWidth, CGFLOAT_MAX)];
        CGRect rect = label.frame;
        rect.size.width = fitSize.width;
        label.frame = rect;
        
        label.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectTag:)];
        [label addGestureRecognizer:tap];
        
        [self.tagLabelList addObject:label];
        
        [self.tagItemView addSubview:label];
        
        UIImageView *tagImageView = [[UIImageView alloc] initWithFrame:CGRectMake(tagImageX, tagImageY, tagImageWidth, tagImageHeight)];
        if (index + 1 < [TAG_IMAGE_LIST count]) {
            tagImageView.image = [UIImage imageNamed:TAG_IMAGE_LIST[index++]];
            [self.tagItemView addSubview:tagImageView];
        }
        
        tagImageY += 50;
    }
    
    self.tagItemView.showsHorizontalScrollIndicator = NO;
    self.tagItemView.showsVerticalScrollIndicator = NO;
    self.tagItemView.contentSize = CGSizeMake(0, tagImageY + 1);
    [self.view bringSubviewToFront:self.tagItemView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)closeNavigation:(UIGestureRecognizer *)sender
{
    NSLog(@"tap: %@", sender);
    [self.delegate closeNavigation];
}

- (IBAction)hideNavigation:(id)sender {
    [self.delegate closeNavigation];
}

- (IBAction)loginIn:(id)sender {
    
}

//- (IBAction)selectTag:(id)sender {
//    UIButton *tagButton = sender;
//    [self.delegate closeNavigation];
//    [self.delegate selectItem:tagButton.titleLabel.text];
//}

- (void)selectTag:(UITapGestureRecognizer *)sender
{
    NSLog(@"%@", sender);
    UILabel *label = (UILabel *)sender.view;
    [self.delegate closeNavigation];
    [self.delegate selectItem:label.text];
}

- (IBAction)login:(id)sender {
    UIButton *loginButton = sender;
    if ([loginButton.titleLabel.text isEqualToString:@"退出"]) {
        [CurrentUser tryLogout];
    } else {
        [CurrentUser tryLogin];
    }
}

@end










